package com.jmb.api.controller.permissionRole;

import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.Result;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.PermissionRole;
import com.jmb.api.service.PermissionRoleService;
import com.jmb.api.vo.permissionRole.PermissionRoleInfoVo;
import com.jmb.api.vo.permissionRole.PermissionRoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Tag(name = "角色权限管理")
@RestController
@RequestMapping("role/permission")
public class PermissionRoleController {
    @Autowired
    private PermissionRoleService permissionRoleService;

    @Operation(summary = "角色权限管理列表")
    @GetMapping
    public Result<List<Integer>> getPermissionRoleList(@RequestParam Integer roleId) {
        List<PermissionRoleInfoVo> list = permissionRoleService.getListByRoleId((roleId));
        List<Integer> permissionIdList = list.stream().map(PermissionRoleInfoVo::getPermissionId)
                .collect(Collectors.toList());
        return Result.ok(permissionIdList);
    }
    @Operation(summary = "角色权限管理新增")
    @PostMapping("save")
    public Result addPermissionRole(@RequestBody PermissionRoleVo permissionRoleVo) {
        permissionRoleService.savePermissionRole(permissionRoleVo);
        return Result.ok();
    }
    @Operation(summary = "角色权限管理删除")
    @PostMapping("remove")
    public Result removePermissionRole(@RequestBody PermissionRoleVo permissionRoleVo) {
        permissionRoleService.removePermissionRole(permissionRoleVo);
        return Result.ok();
    }
}
